package com.example.wwk.graduationpractice.SmackUtils;

import android.util.Log;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.RosterGroup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wwk on 2017/3/19.
 * 获取roster内的用户
 */

public class RosterUtils {        //登录用户后可用
    public static Roster getRoster(AbstractXMPPConnection connection) {
        Roster roster = Roster.getInstanceFor(connection);
        return roster;
    }

    public static Collection<RosterEntry> getEntries(Roster roster) {//直接获取所有用户
        Collection<RosterEntry> entries = roster.getEntries();
        return entries;
    }
    public static List<RosterGroup> getGroups(Roster roster) {//获取所有组
        List<RosterGroup> grouplist = new ArrayList<RosterGroup>();
        Collection<RosterGroup> rosterGroup = roster.getGroups();
        Iterator<RosterGroup> i = rosterGroup.iterator();
        while (i.hasNext()) {
            grouplist.add(i.next());
        }
        return grouplist;
    }
    public static List<RosterEntry> getEntriesByGroup(Roster roster, String groupName) {//获取groupName 组内所有用户

        List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
        RosterGroup rosterGroup = roster.getGroup(groupName);
        Log.d("123", "getEntriesByGroup: "+rosterGroup);
        Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
        Iterator<RosterEntry> i = rosterEntry.iterator();
        while (i.hasNext()) {
            Entrieslist.add(i.next());
        }
        return Entrieslist;
    }
    public static List<RosterEntry> getAllEntries(Roster roster) {
        List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
        Collection<RosterEntry> rosterEntry = roster.getEntries();
        Iterator<RosterEntry> i = rosterEntry.iterator();
        while (i.hasNext()) {
            Entrieslist.add(i.next());
        }
        return Entrieslist;
    }
}
